package com.orangelife.service.propFeeAccountImportLog.impl;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.log4j.Logger;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.Assert;

import com.github.pagehelper.PageHelper;
import com.orangelife.common.MybaitsGenericDaoSubLogic;
import com.orangelife.exception.AppBizException;
import com.orangelife.service.propFeeAccountImportLog.IPropFeeAccountImportLogService;
import com.orangelife.service.propFeeAccountImportLog.model.PropFeeAccountImportLogDto;
import com.orangelife.setting.constant.CodeConst;
import com.orangelife.setting.constant.InfoConst;
import com.orangelife.setting.constant.TypeConst;
import com.orangelife.utils.IdUtil;
import com.orangelife.utils.StringUtils;

/*****************************************************************
 * Copyright(C) ORANGELIFE Corporation CodeConst.BIS_CODE_68001015. All Rights
 * Reserved.
 *****************************************************************/
/*
 * ============================================================== 功能名
 * PropFeeAccountImportLog类的 service层的增删改查
 * 
 * @author @author zhr
 * 
 * @version 1.1.0
 * 
 * @date 2016-08-31 10:03:55
 * ==============================================================
 */
@Service("propFeeAccountImportLogService")
public class PropFeeAccountImportLogServiceImpl extends
		MybaitsGenericDaoSubLogic implements IPropFeeAccountImportLogService {
	private static Logger logger = Logger
			.getLogger(PropFeeAccountImportLogServiceImpl.class);

	public static Logger getLogger() {
		return logger;
	}

	public static void setLogger(Logger logger) {
		PropFeeAccountImportLogServiceImpl.logger = logger;
	}

	@Override
	@Transactional(rollbackFor = { AppBizException.class, Exception.class,
			RuntimeException.class })
	public int addPropFeeAccountImportLog(
			PropFeeAccountImportLogDto propFeeAccountImportLog)
			throws AppBizException {

		Assert.notNull(propFeeAccountImportLog);
		/************************************************
		 * 判断输入参数的长度以及是否能为空
		 ************************************************/

		if (!StringUtils.isBlank(propFeeAccountImportLog.getFileName())) {
			if (propFeeAccountImportLog.getFileName().length() > 256) {
				throw new AppBizException(CodeConst.SYS_CODE_1002, "文件名称长度超出");
			}
		}

		if (!StringUtils.isBlank(propFeeAccountImportLog.getStatus())) {
			if (propFeeAccountImportLog.getStatus().length() > 1) {
				throw new AppBizException(CodeConst.SYS_CODE_1002,
						"1导入中,2上传成功，3上传失败长度超出");
			}
		}

		if (!StringUtils.isBlank(propFeeAccountImportLog.getImportInfo())) {
			if (propFeeAccountImportLog.getImportInfo().length() > 256) {
				throw new AppBizException(CodeConst.SYS_CODE_1002, "上传信息长度超出");
			}
		}

		if (!StringUtils.isBlank(propFeeAccountImportLog.getLink())) {
			if (propFeeAccountImportLog.getLink().length() > 256) {
				throw new AppBizException(CodeConst.SYS_CODE_1002,
						"错误报告日志链接长度超出");
			}
		}

		propFeeAccountImportLog.setImportLogID(IdUtil.getuuId());
		propFeeAccountImportLog.setItemStat(TypeConst.FLAG_ON);
		propFeeAccountImportLog.setTscreate(new Date());
		propFeeAccountImportLog.setTsedit(new Date());
		return this.getSqlSession().insert(
				"PropFeeAccountImportLogMapper.insertSelective",
				propFeeAccountImportLog);
	}

	@Override
	@Transactional(rollbackFor = { AppBizException.class, Exception.class,
			RuntimeException.class })
	public int editPropFeeAccountImportLog(
			PropFeeAccountImportLogDto propFeeAccountImportLog)
			throws AppBizException {

		Assert.notNull(propFeeAccountImportLog);
		/************************************************
		 * 判断输入参数的长度以及是否能为空
		 ************************************************/
		if (StringUtils.isBlank(propFeeAccountImportLog.getImportLogID())) {
			throw new AppBizException(CodeConst.SYS_CODE_1001, "ID不可为空");
		}
		PropFeeAccountImportLogDto oldPropFeeAccountImportLog = this
				.findPropFeeAccountImportLogByID(propFeeAccountImportLog
						.getImportLogID());

		if (!StringUtils.isBlank(propFeeAccountImportLog.getFileName())) {
			if (propFeeAccountImportLog.getFileName().length() > 256) {
				throw new AppBizException(CodeConst.SYS_CODE_1002, "文件名称长度超出");
			}
		}

		if (!StringUtils.isBlank(propFeeAccountImportLog.getStatus())) {
			if (propFeeAccountImportLog.getStatus().length() > 1) {
				throw new AppBizException(CodeConst.SYS_CODE_1002,
						"1导入中,2上传成功，3上传失败长度超出");
			}
		}

		if (!StringUtils.isBlank(propFeeAccountImportLog.getImportInfo())) {
			if (propFeeAccountImportLog.getImportInfo().length() > 256) {
				throw new AppBizException(CodeConst.SYS_CODE_1002, "上传信息长度超出");
			}
		}

		if (!StringUtils.isBlank(propFeeAccountImportLog.getLink())) {
			if (propFeeAccountImportLog.getLink().length() > 256) {
				throw new AppBizException(CodeConst.SYS_CODE_1002,
						"错误报告日志链接长度超出");
			}
		}

		if (oldPropFeeAccountImportLog == null) {
			throw new AppBizException(CodeConst.BIS_CODE_68001,
					InfoConst.BIS_INFO_68001);
		}
		propFeeAccountImportLog.setTsedit(new Date());
		return this.getSqlSession().update(
				"PropFeeAccountImportLogMapper.updateByPrimaryKeySelective",
				propFeeAccountImportLog);
	}

	@Override
	public PropFeeAccountImportLogDto findPropFeeAccountImportLogByID(
			String propFeeAccountImportLogID) throws AppBizException {
		if (StringUtils.isBlank(propFeeAccountImportLogID)) {

			throw new AppBizException(CodeConst.SYS_CODE_1001, "ID不可为空");
		}

		/************************************************
		 * 判断实体是否存在
		 ************************************************/
		PropFeeAccountImportLogDto oldPropFeeAccountImportLog = this
				.getSqlSession()
				.selectOne(
						"PropFeeAccountImportLogSearchMapper.selectByPrimaryKey",
						propFeeAccountImportLogID);
		if (oldPropFeeAccountImportLog == null) {
			throw new AppBizException(CodeConst.BIS_CODE_68001,
					InfoConst.BIS_INFO_68001);
		}

		if (TypeConst.FLAG_OFF.equals(oldPropFeeAccountImportLog.getItemStat())) {

			throw new AppBizException(CodeConst.BIS_CODE_68001,
					InfoConst.BIS_INFO_68001);
		}
		return oldPropFeeAccountImportLog;
	}

	@Override
	@Transactional(rollbackFor = { AppBizException.class, Exception.class,
			RuntimeException.class })
	public int deletePropFeeAccountImportLogByLogic(
			String propFeeAccountImportLogID) throws AppBizException {

		if (StringUtils.isBlank(propFeeAccountImportLogID)) {
			throw new AppBizException(CodeConst.SYS_CODE_1001, "ID不可为空");
		}
		/************************************************
		 * 判断实体是否存在
		 ************************************************/
		PropFeeAccountImportLogDto oldPropFeeAccountImportLog = this
				.findPropFeeAccountImportLogByID(propFeeAccountImportLogID);

		oldPropFeeAccountImportLog.setItemStat(TypeConst.FLAG_OFF);
		oldPropFeeAccountImportLog.setTsedit(new Date());
		return this.getSqlSession().update(
				"PropFeeAccountImportLogMapper.updateByPrimaryKeySelective",
				oldPropFeeAccountImportLog);
	}

	@Override
	public List<Map<String, Object>> findPropFeeAccountImportLogByPage(
			String commID, String tsCreate, String search, int pageNo,
			int pageSize) throws AppBizException {

		/************************************************
		 * 设置输入参数
		 ************************************************/
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("tsCreate", tsCreate);
		map.put("commID", commID);
		map.put("search", search);
		PageHelper.startPage(pageNo, pageSize);
		List<Map<String, Object>> list = this
				.getSqlSession()
				.selectList(
						"PropFeeAccountImportLogSearchMapper.findAllPropFeeAccountImportLog",
						map);
		return list;
	}

	@Override
	public List<PropFeeAccountImportLogDto> getPropFeeAccountImportLogByPage(
			String tsCreate, String search, int pageNo, int pageSize)
			throws AppBizException {
		/************************************************
		 * 设置输入参数
		 ************************************************/
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("tsCreate", tsCreate);
		map.put("search", search);
		PageHelper.startPage(pageNo, pageSize);
		List<PropFeeAccountImportLogDto> list = this
				.getSqlSession()
				.selectList(
						"PropFeeAccountImportLogSearchMapper.getAllPropFeeAccountImportLogDto",
						map);
		return list;
	}

	@Override
	public List<Map<String, Object>> selectPropFeeAccountImportLogByRelevantID(
			String importLogID, String status, String commID)
			throws AppBizException {
		/************************************************
		 * 设置输入参数
		 ************************************************/
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("importLogID", importLogID);
		map.put("status", status);
		map.put("commID", commID);

		List<Map<String, Object>> list = this
				.getSqlSession()
				.selectList(
						"PropFeeAccountImportLogSearchMapper.selectPropFeeAccountImportLogDtoyRelevantID",
						map);
		return list;
	}

	@Override
	public List<PropFeeAccountImportLogDto> findPropFeeAccountImportLogByRelevantID(
			String importLogID, String status, String commID)
			throws AppBizException {
		/************************************************
		 * 设置输入参数
		 ************************************************/
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("importLogID", importLogID);
		map.put("status", status);
		map.put("commID", commID);

		List<PropFeeAccountImportLogDto> list = this
				.getSqlSession()
				.selectList(
						"PropFeeAccountImportLogSearchMapper.findPropFeeAccountImportLogDtoByRelevantID",
						map);
		return list;
	}

	@Override
	public List<PropFeeAccountImportLogDto> bauthfindPropFeeAccountImportLog(
			List<String> propFeeAccountImportLogIDs) throws AppBizException {
		/************************************************
		 * 设置输入参数
		 ************************************************/
		List<PropFeeAccountImportLogDto> list = this
				.getSqlSession()
				.selectList(
						"PropFeeAccountImportLogSearchMapper.batchFindPropFeeAccountImportLog",
						propFeeAccountImportLogIDs);
		return list;
	}

	@Override
	public List<Map<String, Object>> bauthfindPropFeeAccountImportLogByMap(
			Map<String, Object> propFeeAccountImportLog) throws AppBizException {
		/************************************************
		 * 设置输入参数
		 ************************************************/
		List<Map<String, Object>> list = this
				.getSqlSession()
				.selectList(
						"PropFeeAccountImportLogSearchMapper.batchFindPropFeeAccountImportLogByMap",
						propFeeAccountImportLog);
		return list;
	}

	@Override
	public void clearCache() throws AppBizException {
		System.out.println("清除缓存成功");
	}

}